package com.ak47.cms.cms.service

import com.ak47.cms.cms.builder.CrawlerWebClient
import com.ak47.cms.cms.builder.ImageSearchApiBuilder
import com.ak47.cms.cms.dao.ImageRepository
import com.ak47.cms.cms.entity.Image
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.jsoup.Jsoup
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.net.URL
import java.text.SimpleDateFormat
import java.util.*

@Service
class CrawImageService {
    val logger = LoggerFactory.getLogger(CrawImageService::class.java)

    @Autowired
    lateinit var imageRepository: ImageRepository

    fun doSogouImageCrawJob() {

        for (i in 1..10) {
            GlobalScope.launch {
                saveSogouImage(currentPage = i, pageSize = 20)
            }
        }
    }


    fun doCrawHuaBanImages() {
        val boardsUrls = setOf(
                "https://huaban.com/boards/30661962/",
                "https://huaban.com/boards/18976743/",
                "https://huaban.com/boards/24169299/",
                "https://huaban.com/boards/17375733",
                "https://huaban.com/boards/481662/",
                "https://huaban.com/boards/52283153/",
                "https://huaban.com/boards/51202404",
                "https://huaban.com/boards/50385646",
                "https://huaban.com/boards/47342373",
                "https://huaban.com/boards/44920016",
                "https://huaban.com/boards/24345211",
                "https://huaban.com/boards/24329332",
                "https://huaban.com/boards/24169299",
                "https://huaban.com/boards/20194339",
                "https://huaban.com/boards/19570858",
                "https://huaban.com/boards/19457183",
                "https://huaban.com/boards/19443743",
                "https://huaban.com/boards/19374074",
                "https://huaban.com/boards/19367784"
        )
        boardsUrls.forEach {

            GlobalScope.launch { saveHuaBanImages(it) }

        }
    }


    private fun saveHuaBanImages(beautyUrl: String) {
        val crawlerWebClient = CrawlerWebClient()
        val beautyPageHtml = crawlerWebClient.getPage(beautyUrl).asXml()
        val document = Jsoup.parse(beautyPageHtml)

        document.getElementById("waterfall").children().forEach {
            var url = "http:" + it.child(2).child(1).attr("src")
            url = url.replace("_fw236", "_fw1000")
            println("花瓣 url = ${url}")
            if (imageRepository.countByUrl(url) == 0) {
                val image = Image()
                image.category = "花瓣 ${SimpleDateFormat("yyyy-MM-dd").format(Date())}"
                image.url = url
                image.sourceType = 2
                image.imageBlob = getByteArray(url)
                imageRepository.save(image)
            }
        }
    }


    private fun saveSogouImage(currentPage: Int, pageSize: Int) {


        val api = ImageSearchApiBuilder.build(currentPage, pageSize)
        JsonResultProcessor.getSogouImageUrlList(api).forEach {
            val category = it.category
            val url = it.url
            if (imageRepository.countByUrl(url) == 0) {
                val image = Image()
                image.category = category
                image.url = url
                image.sourceType = 0
                image.imageBlob = getByteArray(url)
                try {
                    imageRepository.save(image)
                } catch (e: Exception) {
                }
            }
        }

    }

    private fun getByteArray(url: String): ByteArray {
        val urlObj = URL(url)
        return urlObj.readBytes()
    }
}
